﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web; 

namespace Dooshion.Log
{
    public class Log : ILog
    {
        public string ClassName { get; set; }
        //public static readonly string InfoPath = ConfigurationManager.ConnectionStrings["LogInfoPath"].ToString();
        //public static readonly string ErrorPath = ConfigurationManager.ConnectionStrings["LogErrorPath"].ToString();
        //public static readonly string DebugPath = ConfigurationManager.ConnectionStrings["LogDebugPath"].ToString();

        public static readonly string InfoPath = @"\Log\LogInfo.txt";
        public static readonly string ErrorPath = @"\Log\LogDebug.txt";
        public static readonly string DebugPath = @"\Log\LogError.txt";


        public Log(string className)
        {
            this.ClassName = className;
        }

        /// <summary>
        /// 写入Debug信息
        /// </summary>
        /// <param name="debugMsg"></param>
        public void Debug(string debugMsg)
        {
            string path = HttpContext.Current.Server.MapPath("~/") + DebugPath;
            string msg = "时间：" + DateTime.Now.ToString() + ",位置：" + this.ClassName + ",Debug信息：" + debugMsg;
            WriteTxt(msg, path);
        }


        /// <summary>
        /// 写入Error信息
        /// </summary>
        /// <param name="errorMsg"></param>
        public void Error(string errorMsg)
        {
            string path = HttpContext.Current.Server.MapPath("~/") + ErrorPath;
            string msg = "时间：" + DateTime.Now.ToString() + ",位置：" + this.ClassName + ",Error信息：" + errorMsg;
            WriteTxt(msg, path);
        }


        /// <summary>
        /// 写入说明信息
        /// </summary>
        /// <param name="infoMsg"></param>
        public void Info(string infoMsg)
        {
            string path = HttpContext.Current.Server.MapPath("~/") + InfoPath;
            string msg = "时间：" + DateTime.Now.ToString() + ",位置：" + this.ClassName + ",Info信息：" + infoMsg;
            WriteTxt(msg, path);
        }

        public void WriteTxt(string content, string path)
        {
            if (!FileIsExist(path))
            {
                CreateFile(path);
            }
            using (StreamWriter sr = new StreamWriter(path, true, System.Text.Encoding.GetEncoding("GB2312")))
            {
                sr.WriteLine("\n");
                sr.WriteLine("***************************************************************");
                sr.WriteLine(content);
                sr.Close();
            }
        }


        public void CreateFile(string path)
        {
            using (StreamWriter sr = File.CreateText(path))
            {
                sr.Close();
            }
        }

        public bool FileIsExist(string path)
        {
            return File.Exists(path);
        }
    }
}
